var http = require("http");
var express = require('express');
var app = express();
var mock = require("./__mock__/index.js");
var dataformat=require("./function/index.js")
  console.log(dataformat)

app.all('*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');

    if (req.method == 'OPTIONS') {
        res.send(200);
        /让options请求快速返回/
    } else {
        next();
    }
});
app.get("/mall/", function(req, res) {
    console.log(req.query)
    var _callback = req.query.callback; 
    var _data = "";
    //res.json({ code: "886", error: "缺少ct或者ac参数，我要关机谁家了额!" }); 
    //数据入口
    var DATAFROM = mock.mall;
    if (req.query.ac && req.query.ct) {
        _data = DATAFROM[req.query.ct + "_" + req.query.ac];

        // if(_data&&dataformat[req.query.ct + "_" + req.query.ac]){
            
        //     _data=dataformat[req.query.ct + "_" + req.query.ac](_data,req.query.type)
        // }
    } else {
        _data = { code: "886", error: "缺少ct或者ac参数，我要关机谁家了额!" }
    } 
    if (_callback) {   
        res.type('text/javascript');   
        res.send(_callback + '(' + JSON.stringify(_data) + ')'); 
    }  { 
        res.json(_data); 
    }
});
app.post("/mall/", function(req, res) {
    console.log(req.query)
    var _callback = req.query.callback; 
    var _data = "";
    //res.json({ code: "886", error: "缺少ct或者ac参数，我要关机谁家了额!" }); 
    //数据入口
    var DATAFROM = mock.mall;
    if (req.query.ac && req.query.ct) {
        _data = DATAFROM[req.query.ct + "_" + req.query.ac];
    } else {
        _data = { code: "886", error: "缺少ct或者ac参数，我要关机谁家了额!" }
    } 
    res.json(_data); 
});

var server = require('http').createServer(app);
//指定静态文件的位置
app.use('/static', express.static('static/'));
//监听端口号
server.listen(886);
console.log("Listening at http://localhost:886")
